package com.acme.bankapp.domain;
import com.acme.bankapp.domain.Account;
import com.acme.bankapp.exception.*;
public class CheckingAccount extends Account {
	private double balance;
	private double overdraft;
	private final double overdraftFinal;
	CheckingAccount(double startBalance, double startOverdraft) {
		super();
		this.balance = startBalance;
		this.overdraft = startOverdraft;
		this.overdraftFinal = startOverdraft;
	}
	@Override
	void deposite(double delta) {
		this.balance += delta;
	}
	@Override
	void withdraw(double delta) throws OverDraftLimitExceededException {
		if (this.balance+this.overdraft-delta < -this.overdraftFinal) {
			throw new OverDraftLimitExceededException(this.balance, this.overdraftFinal+this.overdraft+this.balance);
		}
		else {
			if (this.overdraft >= 0) {
				if (this.balance >= delta)
					this.balance -= delta;
				else 
					if (this.overdraft >= (delta - this.balance)) {
						this.overdraft -= delta - this.balance;
						this.balance = 0;
					}
					else {
						this.balance -= delta - this.overdraft;
						this.overdraft = 0;
					}
			}
		}
	}
	@Override
	public double getBalance() {
		return this.balance;
	}
	@Override
	public double maxCredit() {
		return this.overdraftFinal+this.balance+this.overdraft;
	}
}
